package com.longge;

public class MinEatingSpeed {

    public int minEatingSpeed(int[] piles, int h) {
        int left = 1;
        int right = 1000000000;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (feed(piles, mid) == h) {
                right = mid - 1;
            } else if (feed(piles, mid) > h) {
                left = mid + 1;
            } else if (feed(piles, mid) < h) {
                right = mid - 1;
            }
        }
        return right+1;
    }


    public long feed(int[] piles, int x) {
        long res = 0;
        for (int pile : piles) {
            if (pile % x != 0) {
                res += 1;
            }
            res += pile / x;
        }
        return res;
    }

    public static void main(String[] args) {
        int i = new MinEatingSpeed().minEatingSpeed(new int[]{805306368,805306368,805306368
        }, 1000000000);
        System.out.println(i);
    }
}
